package net.lookyou.boot.demo.config.security.handler;

import net.lookyou.boot.demo.common.ApiResult;
import net.lookyou.boot.demo.utils.ResponseUtils;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.authentication.AuthenticationFailureHandler;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/*if (e instanceof UsernameNotFoundException || e instanceof BadCredentialsException) {
    result = ApiResult.fail(e.getMessage());
} else if (e instanceof LockedException) {
    result = ApiResult.fail("账户被锁定，请联系管理员!");
} else if (e instanceof CredentialsExpiredException) {
    result = ApiResult.fail("证书过期，请联系管理员!");
} else if (e instanceof AccountExpiredException) {
    result = ApiResult.fail("账户过期，请联系管理员!");
} else if (e instanceof DisabledException) {
    result = ApiResult.fail("账户被禁用，请联系管理员!");
} else if(e instanceof MyException){
    result = ApiResult.fail("验证码错误!");
}else {
    log.error("登录失败：", e);
    result = ApiResult.fail("登录失败!");
}*/
@Component
public class CustomerAuthenticationFailureHandler implements AuthenticationFailureHandler {
    @Override
    public void onAuthenticationFailure(HttpServletRequest request, HttpServletResponse response, AuthenticationException exception) throws IOException, ServletException {
        ApiResult result = ApiResult.fail(exception.getMessage());
        ResponseUtils.out(response, result);
    }
}
